﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatterns.DecoratorPatterns
{
    class DecoratorPattern
    {
        static void Main(string[] args)
        {
            //装饰者模式，装饰类与组件类完全解耦，动态扩展非常方便，但是嵌套层级太复杂
            //适用于复杂度极大的动态扩展，比继承n多个子类要简单得多

            //实例化组件
            Component com1 = new CorreteComponent();
            //第一次被装饰
            com1 = new ConcreteDecorator1(com1);
            //第二次被装饰
            com1 = new ConcreteDecorator2(com1);
            //执行组件
            com1.operate();
        }
    }
    /// <summary>
    /// 组件抽象类
    /// </summary>
    public abstract class Component {
        public abstract void operate();
    }
    /// <summary>
    /// 组件具体类
    /// </summary>
    public class CorreteComponent : Component
    {
        public override void operate()
        {
            Console.WriteLine("dosomething");
        }
    }
    /// <summary>
    /// 抽象装饰类
    /// </summary>
    public abstract class Decorator : Component {
        private Component component = null;
        public Decorator(Component _component) {
            this.component = _component;
        }

        public override void operate()
        {
            this.component.operate();
        }
    }
    /// <summary>
    /// 具体装饰者1
    /// </summary>
    public class ConcreteDecorator1 : Decorator
    {
        public ConcreteDecorator1(Component _component) : base(_component) {
        }

        private void method1() {
            Console.WriteLine("Decorator1 do something");
        }

        public override void operate()
        {
            base.operate();
            this.method1();
        }
    }
    /// <summary>
    /// 具体装饰者2
    /// </summary>
    public class ConcreteDecorator2 : Decorator
    {
        public ConcreteDecorator2(Component _component) : base(_component)
        {
        }

        private void method2()
        {
            Console.WriteLine("Decorator2 do something");
        }

        public override void operate()
        {
            base.operate();
            this.method2();
        }
    }
}
